﻿<%@ WebHandler Language="C#" Class="UserLinkProcess" %>

using System;
using System.Web;
using System.Web.SessionState;

public class ServiceInfo
{
    public string message { get; set; }

    public String data { get; set; }
}

public class UserInfo
{
    public string user_id { get; set; }

    public string app_user_name { get; set; }

    public string unitid { get; set; }
}

public class UserLinkProcess : IHttpHandler, IRequiresSessionState
{
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    
    /// <summary>
    /// 用户校验逻辑
    /// </summary>
    /// <param name="context"></param>
    public void ProcessRequest(HttpContext context)
    {
        string strReturn = string.Empty;

        //从url获取认证code
        String code = context.Request["code"];
        //实例化OAuthClient类
        OAuthClient oc = new OAuthClient(
            //服务器回调地址
            System.Configuration.ConfigurationManager.AppSettings["OAUTH_SERVER_URI"],
            System.Configuration.ConfigurationManager.AppSettings["APP_ID"],
            System.Configuration.ConfigurationManager.AppSettings["APP_KEY"]
            );
        //获取服务器传回的token（json的字符串），里面包含了办事大厅用户id，业务系统用户名，关联的企业id
        String token = oc.getAccessToken(code);
        //token = "{'message':'1','data':{\'user_id\':\'SUPERADMIN\',\'app_user_name\':\'wangqq\',\'unitid\':\'03F2B852A0E94DE099ACCA645F8C4FD3\'}}";
       
        //验证是否是管理员
        if (!string.IsNullOrEmpty(token))
        {
            ServiceInfo siTemp = Common.JsonOperation.GetObjectFromJson<ServiceInfo>(token);
            if (siTemp.message == "1")
            {
                UserInfo uiTemp = Common.JsonOperation.GetObjectFromJson<UserInfo>(siTemp.data);
                HttpContext.Current.Session["title"] = "国家测绘地理信息局行政许可在线审批系统";
                HttpContext.Current.Session.Remove("Menu");

                //对预审标志初始化
                HttpContext.Current.Session["PromiseFlag"] = string.Empty;
                string strUserId = uiTemp.app_user_name;
                HttpContext.Current.Session["UserID"] = strUserId;
                HttpContext.Current.Session["LoginUserName"] = ChBusiness.User.UserHandle.GetNotes(strUserId);
                RecordLog(strUserId, context);

                context.Response.Redirect("../MainPage.htm");
            }
        }
    }

    #region 记录登录日志
    /// <summary>
    /// 记录登录日志
    /// </summary>
    /// <param name="UserName"></param>
    private void RecordLog(string UserName, HttpContext context )
    {
        string UserIP = context.Request.UserHostName.ToString();
        ChBusiness.Common.SystemLogs wyLog = new ChBusiness.Common.SystemLogs();
        wyLog.Inputlog(UserName, "登录外网申报系统", string.Empty, UserIP);
    }
    #endregion 
}